package kpi.java_training.testing.questions;


import kpi.java_training.testing.education.TypeScore;
import kpi.java_training.testing.services.StringServices;

import java.util.ArrayList;
import java.util.List;

/**
 * class implements question where user chooses several possible variants
 */
public class ManyFromManyQuestion extends IntQuestion {
    private List<Integer> answers;
    public ManyFromManyQuestion(String questionMessage, List<String> answerVariants, List<Integer> correctAnswers,
                                 int level) {
        super(questionMessage, answerVariants, correctAnswers,level);
        answers = null;
    }

    @Override
    public void setAnswer(String answer) {
        setOriginalAnswer(answer);
        String[] strings = StringServices.getWords(answer);
        answers = new ArrayList<Integer>();
        for (int i = 0; i < strings.length; i++) {
            try {
                answers.add(Integer.parseInt(strings[i]));
            } catch (Exception e) {
                answers.add(NOT_ANSWERED);
            }
        }
    }

    @Override
    protected int getQuestionType() {
        return Question.MANY_FROM_MANY;
    }

    @Override
    public boolean isCorrect() {
        return (answers.containsAll(correctAnswers) && (correctAnswers.size() == answers.size()));
    }
    @Override
    protected String getQuestionHelp() {
        return "Type numbers of correct variants divided by spaces.";
    }
}
